package com.kzd.gtgame.jetpack.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.kzd.gtgame.base.BaseViewModel
import com.kzd.gtgame.entity.*
import com.kzd.gtgame.jetpack.manager.IndexRepository
import com.module.lemlin.http.*

class TravelViewModel(application: Application) : BaseViewModel(application) {

    private val mIndexRepository: IndexRepository = IndexRepository()

    private var page: Int = 1
    private var limit: Int = 20

    private var start = 0L

    val travelTodayLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataT<Travel<GameGroup>>>>()
    fun travelToday(isLoadMore: Boolean = false) {
        setRxLifeScope(object : SubscribeViewModel<HttpResponseDataT<Travel<GameGroup>>>(travelTodayLiveData) {
            override fun onSuccess(resp: HttpResponseDataT<Travel<GameGroup>>) {
                super.onSuccess(resp.apply {
                    this.isLoadMore = isLoadMore
                })
                start = resp.data.start
//                HttpResponseDataE(resp.data.list).apply {
//                    code = resp.code
//                    message = resp.message
//                }
//                travelTodayLiveData1.postValue()
            }
        }) {
            if (!isLoadMore) {
                start = 0
            }
            mIndexRepository.travelToday(start.toString())
        }
    }

    val travelWeekLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataE<GameBean>>>()
    fun travelWeek() {
        setRxLifeScope(SubscribeViewModel(travelWeekLiveData)) {
            mIndexRepository.travelWeek()
        }
    }

    val travelReserveLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataT<Travel<GameReserve>>>>()
    fun travelReserve(isLoadMore: Boolean = false) {
        setRxLifeScope(object : SubscribeViewModel<HttpResponseDataT<Travel<GameReserve>>>(travelReserveLiveData) {
            override fun onSuccess(resp: HttpResponseDataT<Travel<GameReserve>>) {
                super.onSuccess(resp.apply {
                    this.isLoadMore = isLoadMore
                })
                start = resp.data.start
                page += 1
            }
        }) {
            if (!isLoadMore) {
                page = 1
                start = 0
            }
            mIndexRepository.travelReserve(start.toString(), page.toString(), limit.toString())
        }
    }

    val reserveChangeLiveData = MutableLiveData<HttpResponseBody<ReserveChangeResponse>>()
    fun reserveChange(gid: String, type: String,position:Int) {
        setRxLifeScope(object :SubscribeViewModel<ReserveChangeResponse>(reserveChangeLiveData){
            override fun onSuccess(resp: ReserveChangeResponse) {
                super.onSuccess(resp.apply { this.position = position })
            }
        }) {
            mIndexRepository.reserveChange(gid, type)
        }
    }

}